package com.yk.business;

import com.yk.pojo.RespMessage;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by dingkl on 2017/7/28.
 */
public class CallBackMsg {

    private RespMessage respMessage;

    private Lock lock = new ReentrantLock();

    private Condition condition = lock.newCondition();

    public Object getResult(){
        Object o = null;
        try {
            lock.lock();
            condition.await(10*1000, TimeUnit.MILLISECONDS);
            if (this.respMessage!=null){
                o = this.respMessage.getData();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            return o;
        }
    }

    public void over(RespMessage respMessage){
        try {
            lock.lock();
            this.respMessage = respMessage;
            condition.signal();
        } finally {
            lock.unlock();
        }
    }

}
